const { baseUrl,http } = require('../../utils/util.js')
// pages/ordereval/ordereval.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: "",
    order_no: "",
    goods_id: "",
    goods_attr_id: "",
    meval:5, //描述评价
    weval:5, //物流服务
    feval:5, //服务态度
    txtval:"",
    list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { order_no, order_id, goods_id, goods_attr_id } = options;
    this.setData({ order_no, order_id, goods_id, goods_attr_id })
  },
  /**
   * 星星点击事件
  */
  handerStarClick(e){
    let star = e.currentTarget.dataset.star;
    let type = e.currentTarget.dataset.type;
    switch(~~type){
      case 1:
         this.setData({
           meval:star,
         })
      break;
      case 2:
        this.setData({
          weval: star,
        })
      break;
      case 3:
        console.log(this)
        this.setData({
          feval: star,
        })
      break;
    }
  },
  //接收文本内容
  acTxtVal(e){
    this.setData({
      txtval:e.detail.value,
    })
  },
  /**
   * 图片选择
  */
  chosePic: function () {
    var _this = this;
    wx.chooseImage({
      count: 3 - (_this.data.list.length),
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        _this.setData({
          list: _this.data.list.concat(tempFilePaths)
        })
      }
    })
  },
  //删除图片
  detelPic(event) {
    var idx = event.currentTarget.dataset.idx;
    var list = this.data.list;
    list.splice(idx, 1)
    this.setData({
      list: list,
    })
  },
  //提交评价
  submitEval(){
    if(this.data.txtval == ""){
      wx.showToast({
        title: '请输入评论内容',
      })
      return false;
    }
    let fp = this.data.list[0]
    let { order_id, order_no, goods_id, goods_attr_id, meval, weval, feval, txtval} = this.data;
    let session_id = wx.getStorageSync('session_id') || "";
    wx.uploadFile({
      url: baseUrl +'/order/comment_order', //仅为示例，非真实的接口地址
      filePath: fp,
      name: 'pic[]',
      header: {
        'Cookie': 'PHPSESSID=' + session_id, //携带存储id
      },
      formData: {
        "order_id": encodeURI(order_id),
        "order_no": encodeURI(order_no),
        "goods_id": encodeURI(goods_id),
        "goods_attr_id": encodeURI(goods_attr_id),
        "star1": encodeURI(meval),
        "star2": encodeURI(weval),
        "star3": encodeURI(feval),
        "content": encodeURI(txtval),
      },
      success(res) {
        let data = JSON.parse(res.data);
        if (data.code == 1) {
          wx.showToast({
            title: '评价成功',
            icon: "success",
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          })
        }
      },
      fail(err){
        wx.showToast({
          title: '评价失败',
          icon: "none",
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})